/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.wizards;

import org.digitalsoul.switchplugin.prefs.util.UITools;

import org.eclipse.swt.widgets.Event;

import org.eclipse.swt.widgets.Listener;


import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public abstract class NewThingWizard extends Wizard {

    private final Page page;
    protected Label labe;
    protected Text textField;
    protected Composite mainPanel;
    protected Button checkbox;

    public NewThingWizard() {
        page = new Page(getPageId());
        addPage(page);
    }

    @Override
    public String getWindowTitle() {
        return getTitle();
    }

    @Override
    public boolean canFinish() {
        return textField.getText().trim().length() > 0 && validateName(textField.getText());
    }

    @Override
    public boolean needsPreviousAndNextButtons() {
        return false;
    }

    protected void setMessage(String msg) {
        page.setMessage(msg);
    }
    
    protected void setErrorMessage(String msg) {
        page.setErrorMessage(msg);
    }
    
    protected abstract String getPageDescription();
    protected abstract String getTitle();
    protected abstract String getPageId();
    protected abstract String getLabelText();
    protected abstract boolean validateName(String text);

    private final class Page extends WizardPage {
        protected Page(String pageId) {
            super(pageId);
        }

        @Override
        public String getDescription() {
            return getPageDescription();
        }

        public void createControl(Composite parent) {
            mainPanel = new Composite(parent, SWT.NONE);
            GridLayout gridLayout = new GridLayout(2, false);
            gridLayout.marginTop = 12;
            gridLayout.marginLeft = 12;
            mainPanel.setLayout(gridLayout);
            setControl(mainPanel);
            Label label = new Label(mainPanel, SWT.NONE);
            label.setText(getLabelText());
            textField = UITools.addTextField(mainPanel);
            textField.addListener(SWT.Modify, new Listener() {
                public void handleEvent(Event arg0) {
                    getContainer().updateButtons();
                }
            });
        }
    }
}
